//
//  SelectList.m
//  Trading365
//
//  Created by yifei tong on 12-2-8.
//  Copyright (c) 2012年 dhc. All rights reserved.
//

#import "InfoSelectList.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "Session.h"
#import "InfoType.h"


@implementation InfoSelectList
@synthesize table;
@synthesize list;
@synthesize lastIndexPath;
@synthesize selectProductArray;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init
{
	self = [super init];
	if(self)
	{
//        responseParams = [[NSMutableArray alloc] initWithCapacity:0];
        indexpathDic = [[NSMutableDictionary alloc] initWithCapacity:0];
//        selectProductArray = [[NSMutableArray alloc] initWithCapacity:0];
	}
	
	return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [table release];
    [search release];
    [list release];
    [lastIndexPath release];
//    [responseParams release];
    [selectProductArray release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [self init];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.search.showsCancelButton=NO;
    
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"返回" 
                                   style:UIBarButtonItemStyleDone
                                   target:self
                                   action:@selector(goBack:)];
    self.navigationItem.leftBarButtonItem = saveButton;
    [saveButton release];

//    
//    if (infotype == INFO_TYPE_PRODUCT) {
//        UIBarButtonItem *newButton = [[UIBarButtonItem alloc]
//                                      initWithTitle:@"完成"
//                                      style:UIBarButtonItemStyleBordered
//                                      target:self
//                                      action:@selector(AddArray)];
//        self.navigationItem.rightBarButtonItem = newButton;
//        [newButton release];
//    }
        
    [self refreshData];
    [self.table reloadData];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    table=nil;
    search = nil;
    
    [[[TaskManager Share] GetTaskByType:GetInfoList Delegate:self] cancel];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)goBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - Table view data source

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [responseParams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier: CellIdentifier] autorelease];
    }
    
    NSUInteger row = [indexPath row];
    if (row >= [responseParams count]) {
        return cell;
    }
    
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:18];
    NSMutableDictionary *tmp_dictionary = [[NSMutableDictionary alloc] initWithDictionary:[responseParams objectAtIndex:row]];

    //产品类别
    if (infotype == INFO_TYPE_PRODUCT_CATEGORY) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@"product_category_name"];        
    }

    //@"单位",
    if (infotype == INFO_TYPE_PRODUCT_UNIT) {
        cell.textLabel.text =[tmp_dictionary objectForKey:@"unit_name_cn"];        
    }

    //@"海关编码",
    if (infotype == INFO_TYPE_PRODUCT_CUSTOM_CODE) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@"customs_prod_name_cn"];        
    }
   
    //@"客户",
    if (infotype == INFO_TYPE_CUSTOMER) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@"customer_name_cn"];        
    }
    
    //@"yewuyuan",
    if (infotype == INFO_TYPE_EMPLOYE) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@"user_name_cn"];        
    }
    
    
    //@"",
    if (infotype == INFO_TYPE_CONTACT) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@""];        
    }

    //@"shangji",
    if (infotype == INFO_TYPE_CHANCE) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@"chance_name"];        
    }

    //@"bizhong",
    if (infotype == INFO_TYPE_CURRENCY_TYPE) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@""];        
    }

    if (infotype == INFO_TYPE_PRICE_TERMS) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@""];        
    }
    
    if (infotype == INFO_TYPE_DEPARTURE_PORT) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@""];        
    }
    
    if (infotype == INFO_TYPE_ARRIVAL_PORT) {        
        cell.textLabel.text =[tmp_dictionary objectForKey:@""];        
    }
    
    [tmp_dictionary release];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    int newRow = [indexPath row];
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    NSMutableDictionary *tmp_dictionary = [[NSMutableDictionary alloc] initWithDictionary:[responseParams objectAtIndex:newRow]];
    NSString *p_id=[NSString stringWithString:[tmp_dictionary objectForKey:@"id"]];
    [tmp_dictionary release];
    
    if (newRow != oldRow)
    {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        //@"单位",
        if (infotype == INFO_TYPE_PRODUCT_UNIT) {
            [[[InfoType share] GetTranValueDict] setValue:newCell.textLabel.text forKey:INFO_NAME_PRODUCT_UNIT];
            [[[InfoType share] GetTranValueDict] setValue:p_id forKey:INFO_NAME_PRODUCT_UNIT_ID];
        }

        //@"产品类别",
        if (infotype == INFO_TYPE_PRODUCT_CATEGORY){
            [[[InfoType share] GetTranValueDict] setValue:newCell.textLabel.text forKey:INFO_NAME_PRODUCT_CATEGORY];
            [[[InfoType share] GetTranValueDict] setValue:p_id forKey:INFO_NAME_PRODUCT_CATEGORY_ID];
        }

        //@"海关编码",
        if (infotype == INFO_TYPE_PRODUCT_CUSTOM_CODE){
            [[[InfoType share] GetTranValueDict] setValue:newCell.textLabel.text forKey:INFO_NAME_PRODUCT_CUSTOM_CODE];
            [[[InfoType share] GetTranValueDict] setValue:p_id forKey:INFO_NAME_PRODUCT_CUSTOM_CODE_ID];            
        }

        //@"客户",
        if (infotype == INFO_TYPE_CUSTOMER){
            [[[InfoType share] GetTranValueDict] setValue:newCell.textLabel.text forKey:INFO_NAME_CHANCE_CUSTOMER_NAME];
            [[[InfoType share] GetTranValueDict] setValue:p_id forKey:INFO_NAME_CHANCE_CUSTOMER_NAME_ID];            
        }

        //@"业务员",
        if (infotype == INFO_TYPE_EMPLOYE){
            [[[InfoType share] GetTranValueDict] setValue:newCell.textLabel.text forKey:INFO_NAME_CHANCE_EMPLOYE_NAME];
            [[[InfoType share] GetTranValueDict] setValue:p_id forKey:INFO_NAME_CHANCE_EMPLOYE_NAME_ID];            
        }

        
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        self.lastIndexPath = indexPath;
    }
        
    //点击条目即返回
//    [self.navigationController popViewControllerAnimated:YES];
//    NSLog(@"[[InfoType share] SetTranValueDict]=%@",[[InfoType share] GetTranValueDict]);

}

#pragma mark - 
#pragma mark - my function
- (void)doSearch{
    [super doSearch];
    //
    [self.table reloadData];
    
    [[[TaskManager Share] GetTaskByType:GetInfoSelectList Delegate:self] Excute];
    
}

-(void) setAtRow:(int)row
{
    atRow=row;
}
-(int) getAtRow
{
    return atRow;
}


-(void)AddArray
{
    NSMutableDictionary *dict = nil;
    [self.selectProductArray removeAllObjects];
    
    for(id key in indexpathDic)
    {
        dict=[indexpathDic objectForKey:key];

        [self.selectProductArray addObject:dict];
    }

    [self.navigationController popViewControllerAnimated:YES];
}

-(void)SetInfoType:(int)type
{
    infotype = type;
}

-(void)refreshData
{
     //设置标题
    if (infotype == INFO_TYPE_PRODUCT_CATEGORY){
        self.title = INFO_STRING_PRODUCT_CATEGORY;
    }
    if (infotype == INFO_TYPE_CUSTOMER){
        self.title = INFO_STRING_CUSTOMER;
    }
    if (infotype == INFO_TYPE_PRODUCT){
        self.title = INFO_STRING_PRODUCT;
    }
    
    if (infotype == INFO_TYPE_EMPLOYE){
        self.title =INFO_STRING_EMPLOYE;
    }
    
    if (infotype == INFO_TYPE_CONTACT){
        self.title =INFO_STRING_CONTACT;
    }
 
    if (infotype == INFO_TYPE_CURRENCY_TYPE){
        self.title =INFO_STRING_CURRENCY;
    }
    if (infotype == INFO_TYPE_PRICE_TERMS){
        self.title =INFO_STRING_PRICE_TERMS;
    }
    if (infotype == INFO_TYPE_DEPARTURE_PORT){
        self.title =INFO_STRING_DEPARTURE_PORT;
    }
    if (infotype == INFO_TYPE_ARRIVAL_PORT){
        self.title =INFO_STRING_ARRIVAL_PORT;
    }

    
    [[[TaskManager Share] GetTaskByType:GetInfoSelectList Delegate:self] Excute];
}

-(void) getMoreData
{
    [[[TaskManager Share] GetTaskByType:GetInfoSelectList Delegate:self] Excute];
}

-(void) OnPreExcute
{
    [super OnPreExcute];
}

-(void) DoInBackground
{
    if (getflag == NO && _isSearching == NO) {
        return;
    }
    startRow = [responseParams count];
    NSString *num = [NSString stringWithFormat:@"%i",startRow];
    NSString *rowsperpage = [NSString stringWithFormat:@"%i",PER_PAGE_ROWS];
    NSMutableArray *tmp=nil;
    XmlRpc *xml = [[XmlRpc alloc] init];
    
    switch (infotype) {
        case INFO_TYPE_CUSTOMER:     //客户信息
        {   
            if (_isSearching){
                tmp = [xml GetSearchCustomerList:[[Session share] getMemberID] PosiID:[[Session share] getMemberPosiID] Type:@"01" searchWord:p_search startRows:num rowsPerPage:rowsperpage];              
            }else{
                tmp = [xml GetSearchCustomerList:[[Session share] getMemberID] PosiID:[[Session share] getMemberRoleID] Type:@"02" searchWord:p_search startRows:num rowsPerPage:rowsperpage];              
            }
            break;
        }    
        case INFO_TYPE_EMPLOYE:     //yewuyuan
        {   
            if (_isSearching){
                tmp = [xml GetEmployeList:num  rowsPerPage:rowsperpage];              
            }else{
                tmp = [xml GetEmployeList:num  rowsPerPage:rowsperpage];
            }
            break;
        }    

//        case INFO_TYPE_PRODUCT:     //产品信息
//        {   
//            if (_isSearching){
//                tmp = [xml GetProductList:num  rowsPerPage:rowsperpage];                
//            }else{
//                tmp = [xml GetProductList:num  rowsPerPage:rowsperpage];
//            }
//            break;
//        }    
            
        case INFO_TYPE_PRODUCT_CATEGORY://产品类别
        {   
            if (_isSearching){
                tmp = [xml GetSearchProductCategoryList:p_search startRows:num rowsPerPage:rowsperpage];                
            }else{
                tmp = [xml GetProductCategoryList:num rowsPerPage:rowsperpage];
            }            
            break;
        }    

        case INFO_TYPE_PRODUCT_UNIT://产品单位
        {   
            if (_isSearching){
                tmp = [xml GetSearchProductUnitList:p_search startRows:num rowsPerPage:rowsperpage];
            }else{
                tmp = [xml GetProductUnitList:num rowsPerPage:rowsperpage];
            }
            break;
        }    

        case INFO_TYPE_PRODUCT_CUSTOM_CODE://海关编码
        {   
            if (_isSearching){
                tmp = [xml GetSearchProductCustomsCodeList:p_search startRows:num rowsPerPage:rowsperpage];
            }else{
                tmp = [xml GetProductCustomsCodeList:num rowsPerPage:rowsperpage];
            }
            break;
        }    
                        
        case INFO_TYPE_CURRENCY_TYPE:
        {   
            break;
        }    
            
        case INFO_TYPE_PRICE_TERMS:
        {   
            break;
        }    

        case INFO_TYPE_DEPARTURE_PORT:
        {   
            break;
        }    
            
        case INFO_TYPE_ARRIVAL_PORT:
        {   
            break;
        }    
            

        default:
            break;
    }
    
    for (NSMutableDictionary* itq in tmp) {
        [responseParams addObject:itq];
    }
    if ([tmp count]<PER_PAGE_ROWS) {
        getflag = NO;
    }

    [xml release];
    
    [self.table reloadData];
}

-(void) OnPostExcute
{
    [super OnPostExcute];
}


@end
